package com.xbongbong.paas.controller.workflow;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.workflow.nodes.dto.WorkflowNodeBackDTO;
import com.xbongbong.paas.pojo.workflow.nodes.dto.WorkflowNodeCopyDTO;
import com.xbongbong.paas.pojo.workflow.nodes.dto.WorkflowNodeDeleteDTO;
import com.xbongbong.paas.pojo.workflow.nodes.dto.WorkflowNodeGetDTO;
import com.xbongbong.paas.pojo.workflow.nodes.dto.WorkflowNodeListDTO;
import com.xbongbong.paas.pojo.workflow.nodes.dto.WorkflowNodeSortDTO;
import com.xbongbong.paas.pojo.workflow.nodes.dto.WorkflowNodeSourceListDTO;
import com.xbongbong.paas.pojo.workflow.nodes.dto.WorkflowNodeUpdateDTO;
import com.xbongbong.paas.pojo.workflow.nodes.dto.WorkflowNodeViewDetailDTO;
import com.xbongbong.paas.pojo.workflow.nodes.vo.WorkflowNodeBackVO;
import com.xbongbong.paas.pojo.workflow.nodes.vo.WorkflowNodeCopyVO;
import com.xbongbong.paas.pojo.workflow.nodes.vo.WorkflowNodeDeleteVO;
import com.xbongbong.paas.pojo.workflow.nodes.vo.WorkflowNodeGetVO;
import com.xbongbong.paas.pojo.workflow.nodes.vo.WorkflowNodeListVO;
import com.xbongbong.paas.pojo.workflow.nodes.vo.WorkflowNodeSaveVO;
import com.xbongbong.paas.pojo.workflow.nodes.vo.WorkflowNodeSortVO;
import com.xbongbong.paas.pojo.workflow.nodes.vo.WorkflowNodeSourceListVO;
import com.xbongbong.paas.pojo.workflow.nodes.vo.WorkflowNodeUpdateVO;
import com.xbongbong.paas.pojo.workflow.nodes.vo.WorkflowNodeViewDetailVO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowNodeSaveDTO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.workflow.setting.WorkflowNodeService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description: 工作流节点相关
 * @author 吴峰
 * @date 2021/5/10 14:44
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/workflowNode")
public class WorkflowNodeController {

    @Resource
    private WorkflowNodeService workflowNodeService;
    @Resource
    private PaasRedisHelper paasRedisHelper;



    /**
     *
     * @param workflowNodeSaveDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/17 10:10
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid WorkflowNodeSaveDTO workflowNodeSaveDTO, BindingResult br) {
        XbbResponse<WorkflowNodeSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(workflowNodeSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(workflowNodeSaveDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                WorkflowNodeSaveVO workflowNodeSaveVO = workflowNodeService.save(workflowNodeSaveDTO);
                response = new XbbResponse<>(workflowNodeSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除节点
     * @param workflowNodeDeleteDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/17 10:10
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid WorkflowNodeDeleteDTO workflowNodeDeleteDTO, BindingResult br) {
        XbbResponse<WorkflowNodeDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowNodeDeleteVO workflowNodeDeleteVO = workflowNodeService.delete(workflowNodeDeleteDTO);
                response = new XbbResponse<>(workflowNodeDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取节点list
     * @param workflowNodeListDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/17 10:10
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid WorkflowNodeListDTO workflowNodeListDTO, BindingResult br) {
        XbbResponse<WorkflowNodeListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowNodeListVO workflowNodeListVO = workflowNodeService.list(workflowNodeListDTO);
                response = new XbbResponse<>(workflowNodeListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取节点详情
     * @param workflowNodeGetDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/25 9:29
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid WorkflowNodeGetDTO workflowNodeGetDTO, BindingResult br) {
        XbbResponse<WorkflowNodeGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowNodeGetVO workflowNodeGetVO = workflowNodeService.get(workflowNodeGetDTO);
                response = new XbbResponse<>(workflowNodeGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 获取节点详情
     * @param workflowNodeGetDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/25 9:29
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sort(@RequestBody @Valid WorkflowNodeSortDTO workflowNodeGetDTO, BindingResult br) {
        XbbResponse<WorkflowNodeSortVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowNodeSortVO workflowNodeGetVO = workflowNodeService.sort(workflowNodeGetDTO);
                response = new XbbResponse<>(workflowNodeGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 新建数据节点获取数据源
     * 
     * @param workflowNodeGetDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/6/17 16:59
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/sourceNodeList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sort(@RequestBody @Valid WorkflowNodeSourceListDTO workflowNodeGetDTO, BindingResult br) {
        XbbResponse<WorkflowNodeSourceListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowNodeSourceListVO workflowNodeGetVO = workflowNodeService.sourceNodeList(workflowNodeGetDTO);
                response = new XbbResponse<>(workflowNodeGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 新建数据节点获取数据源
     *
     * @param workflowNodeUpdateDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/6/17 16:59
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid WorkflowNodeUpdateDTO workflowNodeUpdateDTO, BindingResult br) {
        XbbResponse<WorkflowNodeUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowNodeUpdateVO workflowNodeGetVO = workflowNodeService.update(workflowNodeUpdateDTO);
                response = new XbbResponse<>(workflowNodeGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 审批节点，回退按钮校验
     *
     * @param workflowNodeBackDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/6/17 16:59
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/back", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String back(@RequestBody @Valid WorkflowNodeBackDTO workflowNodeBackDTO, BindingResult br) {
        XbbResponse<WorkflowNodeBackVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowNodeBackVO workflowNodeBackVO = workflowNodeService.back(workflowNodeBackDTO);
                response = new XbbResponse<>(workflowNodeBackVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 复制节点
     *
     * @param workflowNodeCopyDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/6/17 16:59
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/copy", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String copy(@RequestBody @Valid WorkflowNodeCopyDTO workflowNodeCopyDTO, BindingResult br) {
        XbbResponse<WorkflowNodeCopyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowNodeCopyVO workflowNodeCopyVO = workflowNodeService.copy(workflowNodeCopyDTO);
                response = new XbbResponse<>(workflowNodeCopyVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 复制该条件下的所有节点
     *
     * @param workflowNodeCopyDTO
     * @param br
     * @return
     * @throws
     * @author 汪腾伟
     * @date 2022/6/9 16:59
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/copyAllNode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String copyAllNode(@RequestBody @Valid WorkflowNodeCopyDTO workflowNodeCopyDTO, BindingResult br) {
        XbbResponse<WorkflowNodeCopyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowNodeCopyVO workflowNodeCopyVO = workflowNodeService.copyAllNode(workflowNodeCopyDTO);
                response = new XbbResponse<>(workflowNodeCopyVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 消息节点，穿透详情
     *
     * @param workflowNodeViewDetailDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/6/17 16:59
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/sendMsgNode/viewDetail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String viewDetail(@RequestBody @Valid WorkflowNodeViewDetailDTO workflowNodeViewDetailDTO, BindingResult br) {
        XbbResponse<WorkflowNodeViewDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowNodeViewDetailVO workflowNodeCopyVO = workflowNodeService.viewDetail(workflowNodeViewDetailDTO);
                response = new XbbResponse<>(workflowNodeCopyVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
